package com.simple.simple_view.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.simple.simple_view.domain.ResponseResult;
import com.simple.simple_view.domain.dto.CommentDto;
import com.simple.simple_view.domain.entity.Comment;
import com.simple.simple_view.domain.system.RabbitMQConstant;
import com.simple.simple_view.domain.system.SystemParam;
import com.simple.simple_view.mapper.CommentMapper;
import com.simple.simple_view.service.CommentService;
import com.simple.simple_view.utils.BeanCopyUtil;
import com.simple.simple_view.utils.SecurityUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2023-11-02 17:58:14
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Resource
    RabbitTemplate rabbitTemplate;
    @Override
    public ResponseResult publishComment(CommentDto commentDto) {
        commentDto.setUserId(SecurityUtil.getUserId());
        Comment comment = BeanCopyUtil.copyBean(commentDto, Comment.class);
        rabbitTemplate.convertAndSend(RabbitMQConstant.QUEUE_COMMENT, comment);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult getCommentPage(int currentPage, int pageSize) {
        Page<Comment>commentPage =new Page<>(currentPage, pageSize);
        LambdaQueryWrapper<Comment>wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getToCommentId, SystemParam.ROOT_COMMENT_SIGN);
        wrapper.orderByDesc(Comment::getCreateTime);
        Page<Comment> page = page(commentPage, wrapper);
        return ResponseResult.okResult(page);
    }
}
